package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.constant.TeachingArrangeBizTypeConstant;
import com.bt.lms.entity.TeachingArrangeInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.TeachingArrangeService;
import com.bt.lms.service.impl.TeachingArrangeServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.SQLException;

/**
 * 教学安排模块 控制器
 *
 * @author 朱传博
 */
public class TeachingArrangeModuleHandler implements ModuleHandler {


    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {

        //获取业务类型
        Short bizType = reqMsg.getBizType();

        //处理数据
        String data = reqMsg.getData();

        //调用业务接口
        Object obj = callBizType(bizType, data);

        //获取2way
        Byte twoWay = reqMsg.getTwoWay();

        if (twoWay == ReqMsg.NETWORK_TWO_WAY_CHAT) {

            //组响应消息
            RespMsg respMsg = objToRespMsg(reqMsg, obj);

            //响应消息转成byte数组
            byte[] bytes = respMsg.toBytes();

            //回应客户端
            try {
                OutputStream out = socket.getOutputStream();
                out.write(bytes);
                out.flush();


            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }

    }

    /**
     * 对象转响应消息
     *
     * @param reqMsg
     * @param obj
     * @return
     */
    private RespMsg objToRespMsg(ReqMsg reqMsg, Object obj) {


        //响应消息
        RespMsg respMsg = new RespMsg();

        //魔数
        String magicNo = reqMsg.getMagicNo();
        respMsg.setMagicNo(magicNo);

        //协议版本号
        Byte version = reqMsg.getVersion();
        respMsg.setVersion(version);

        //序列化算法
        Byte serialCou = reqMsg.getSerialCou();
        respMsg.setSerialCou(serialCou);

        //事件类型
        Byte evenType = reqMsg.getEvenType();
        respMsg.setEvenType(evenType);

        //业务类型
        Short bizType = reqMsg.getBizType();
        respMsg.setBizType(bizType);

        //状态
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);

        //请求id
        Long reqId = reqMsg.getReqId();
        respMsg.setReqId(reqId);

        //保留字段
        byte[] remainField = reqMsg.getRemainField();
        respMsg.setRemainField(remainField);

        //数据
        String data = JSON.toJSONString(obj);
        respMsg.setData(data);


        return respMsg;


    }

    /**
     * 调用业务接口
     *
     * @param bizType 业务类型
     * @param data    数据
     * @return 对象
     */
    private Object callBizType(Short bizType, String data) {


        Object obj = null;
        //调用业务接口
        TeachingArrangeService teachingArrangeService = new TeachingArrangeServiceImpl();
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_ADD)) {

            TeachingArrangeInfo teachingArrangeInfo = JSON.parseObject(data, TeachingArrangeInfo.class);

            try {
                obj = teachingArrangeService.add(teachingArrangeInfo);


            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
//                throw new RuntimeException(e);
            }
        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_ADD_AFFIRM_COURSE)) {
            TeachingArrangeInfo teachingArrangeInfoAddAffirm = JSON.parseObject(data, TeachingArrangeInfo.class);
            try {
                obj = teachingArrangeService.addAffirmCourseStart(teachingArrangeInfoAddAffirm);
            } catch (SQLException e) {
//                    throw new RuntimeException(e);
            }

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_EDIT)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_QUERY_DETAIL)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_DELETE)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_AFFIRM_COURSE)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_Paging_QUERY_NO_START)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_Paging_QUERY_PROCESS)) {

        }
        if (bizType.equals(TeachingArrangeBizTypeConstant.TEACHING_ARRANGE_INFO_Paging_QUERY_FINISH)) {

        }


        return obj;


    }
}
